package com.android.djlmapp.activity.my.set;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.djlmapp.R;
import com.android.djlmapp.adapter.my.MyBaseRecyclerViewAdapter;
import com.android.djlmapp.adapter.my.MyPerRecyclerviewAdapter;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.djlmapp.tools.my.PermissionsLogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class PermissionActivity extends BaseActivity {
    public static final String TAG = "djlm-my: ";

    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.my_setting_per_rec)
    RecyclerView mySettingPerRec;

    List<HashMap<String, Object>> mData;
    private Unbinder mUnbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        mUnbinder = ButterKnife.bind(this);

        initData();
        initView();
    }

    @Override
    protected void initView() {

        homeIvReadpoint.setVisibility(View.GONE);
        topNotes.setVisibility(View.GONE);
        tvName.setText("权限管理");

        Log.i(TAG, mData.toString());

        MyPerRecyclerviewAdapter myRecyclerviewAdapter = new MyPerRecyclerviewAdapter(mData, this, R.layout.my_per_item);
        mySettingPerRec.setAdapter(myRecyclerviewAdapter);
        mySettingPerRec.setLayoutManager(new LinearLayoutManager(this));

        myRecyclerviewAdapter.setOnItemClickListener(new MyBaseRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //此处进行监听事件的业务处理
//                Toast.makeText(getActivity(), "我是item" + position, Toast.LENGTH_SHORT).show();

                //权限开启
                openPermission(position);
            }
        });

    }

    @Override
    protected void initData() {
        //权限检测
        mData = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap();
        hashMap.put("name", "网络权限");
        hashMap.put("status", PermissionsLogUtils.isAppliedPermission(this,
                Manifest.permission.INTERNET));
        mData.add(hashMap);

        hashMap = new HashMap();
        hashMap.put("name", "Wifi权限");
        hashMap.put("status", PermissionsLogUtils.isAppliedPermission(this,
                Manifest.permission.ACCESS_WIFI_STATE));
        mData.add(hashMap);

        hashMap = new HashMap();
        hashMap.put("name", "文件读写权限");
        hashMap.put("status", PermissionsLogUtils.checkPermissions(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE));
        mData.add(hashMap);

        hashMap = new HashMap();
        hashMap.put("name", "打电话权限");
        hashMap.put("status", PermissionsLogUtils.isAppliedPermission(this,
                Manifest.permission.CALL_PHONE));
        mData.add(hashMap);

        hashMap = new HashMap();
        hashMap.put("name", "摄像头权限");
        hashMap.put("status", PermissionsLogUtils.isAppliedPermission(this,
                Manifest.permission.CAMERA));
        mData.add(hashMap);

        hashMap = new HashMap();
        hashMap.put("name", "定位权限");
        hashMap.put("status", PermissionsLogUtils.checkPermissions(this,
                Manifest.permission.ACCESS_COARSE_LOCATION,//网络定位
                Manifest.permission.ACCESS_FINE_LOCATION, //GPS定位
                Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS));//访问定位额外命令
        mData.add(hashMap);
    }

    public void openPermission(int position) {
        if(! (Boolean) mData.get(position).get("status")){
            ToastUtils.showShortToastSafe(getActivity(), "权限不足,您没有开启此权限。请到系统设置-->权限管理 开启权限。");
        }
    }


    @OnClick({R.id.ll_top_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_top_back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
